package com.xindaoapp.framework.activity;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.picasso.Picasso;
import com.xindaoapp.framework.R;
import com.xindaoapp.framework.api.NewApiImpl;
import com.xindaoapp.framework.bean.Tuan;
import com.xindaoapp.framework.bean.Tuan.Data.SecKillGood;
import com.xindaoapp.framework.utils.IRequest;
import com.xindaoapp.framework.view.TuanCustomDigitalClock;

public class TuanActivity extends BaseActivity {
	private PullToRefreshListView pull;
	private ListView listview;
	private RelativeLayout no_result;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_refresh);
		setTitle("团购");
		initUI();
		initTuans(page);
	}
	private void initUI() {
		no_result=(RelativeLayout) findViewById(R.id.no_result);
		pull=(PullToRefreshListView) findViewById(R.id.pull);
		impl = NewApiImpl.getInstance();
		listview=pull.getRefreshableView();
		pull.setMode(Mode.DISABLED);
		pull.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
			@Override
			public void onLastItemVisible() {
				if(total==tuan.size()){
					Toast.makeText(getApplicationContext(), "没有更多数据了", Toast.LENGTH_SHORT).show();
					pull.onRefreshComplete();
				}else{
					initTuans(++page);
				}
			}
		});
		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				
			}
		});
	}
	private NewApiImpl impl;
	private int total;
	private int page=1;
	private TuanAdapter adapter;
	private List<SecKillGood> tuan=new ArrayList<SecKillGood>();
	private void initTuans(int page2) {
		impl.getTuan(page2, 10, new IRequest<String>() {
			@Override
			public void request(String t) {
				if(t.equals("error")){
					
				}else{
					try {
						Tuan parse = JSON.parseObject(t, Tuan.class);
						if(parse.getStatus().equals("1")){
							List<SecKillGood> starting = parse.data.info;
							tuan.addAll(starting);
							if(adapter==null){
								adapter=new TuanAdapter(tuan);
								listview.setAdapter(adapter);
							}else{
								adapter.notifyDataSetChanged();
							}
							if(Integer.parseInt(parse.data.count)<=15){
								pull.setMode(Mode.DISABLED);
							}
						}
						if(parse.getStatus().equals("1")){
							pull.setVisibility(View.VISIBLE);
							no_result.setVisibility(View.GONE);
							if(parse.data.info.size()==0&&tuan.size()==0){
								no_result.setVisibility(View.VISIBLE);
								pull.setVisibility(View.GONE);
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
						no_result.setVisibility(View.VISIBLE);
						pull.setVisibility(View.GONE);
					}
				}
				pull.onRefreshComplete();
			}
		});
	}
	private class TuanAdapter extends BaseAdapter{
		private List<SecKillGood> tuan1;
		public TuanAdapter(List<SecKillGood> tuan1) {
			super();
			this.tuan1 = tuan1;
		}
		@Override
		public int getCount() {
			return tuan1.size();
		}
		@Override
		public Object getItem(int position) {
			return tuan1.get(position);
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if(convertView==null){
				holder=new ViewHolder();
				convertView=View.inflate(TuanActivity.this, R.layout.good_tuan_item, null);
				holder.good_pic=(ImageView) convertView.findViewById(R.id.good_pic);
				holder.good_name=(TextView) convertView.findViewById(R.id.good_name);
				holder.good_shop_price=(TextView) convertView.findViewById(R.id.good_shop_price);
				holder.good_market_price=(TextView) convertView.findViewById(R.id.good_market_price);
				holder.good_sell=(TextView) convertView.findViewById(R.id.good_sell);
				holder.good_surplus=(TextView) convertView.findViewById(R.id.good_surplus);
				holder.tuan_time =(TuanCustomDigitalClock) convertView.findViewById(R.id.tuan_time);
				holder.tuancount=(TextView) convertView.findViewById(R.id.tuancount);
				convertView.setTag(holder);
			}else{
				holder=(ViewHolder) convertView.getTag();
			}
			holder.good_name.setText(tuan1.get(position).goods_name);
			holder.good_shop_price.setText(formatPrice(Float.valueOf(tuan1.get(position).ext_infos.price_ladder.get(0).price)));
			holder.good_market_price.setText("￥"+formatPrice(Float.valueOf(tuan1.get(position).market_price)));
			holder.good_market_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
			holder.good_sell.setText(formatDaZhe(Float.parseFloat(formatDaZhe(Float.valueOf(tuan1.get(position).ext_infos.price_ladder.get(0).price)*10/Float.valueOf(tuan1.get(position).market_price))))+"折");
			holder.good_surplus.setText(tuan1.get(position).goods_number);
			
			holder.tuancount.setText(tuan1.get(position).sold_number+"人已团购");
			holder.tuan_time.setEndTime((formatDate(tuan1.get(position).end_time)));
			Picasso.with(TuanActivity.this).load(tuan1.get(position).goods_thumb)
			.into(holder.good_pic);
			return convertView;
		}
	}
	private String formatDaZhe(Float a) {
		DecimalFormat df = new DecimalFormat("0.0");
		return df.format(a);
	}
	private String formatPrice(Float a) {
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(a);
	}

	class ViewHolder{
		ImageView good_pic;
		TextView good_name;
		TextView good_shop_price;
		TextView good_market_price;
		TextView good_sell;
		TextView good_surplus;
		TextView tuancount;
		TuanCustomDigitalClock tuan_time;
	}
	public static long formatDate(String data) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return sdf.parse(data).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
}
